//#include <fstream>
#include <math.h>
#include "optimisations.h"
#include <iostream>
#include <fstream>
using namespace std;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//											Save - Image
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned char *magic;
filebuf fb;

void Write_Short(unsigned short data, ostream* outFile) { 
	outFile->write((const char*)&data, sizeof(data)); 
}
void Write_Long(unsigned long data, ostream* outFile) { 
	outFile->write((const char*)&data, sizeof(data)); 
}

void save()
{
	//int pad = (4 -((render_width * 3) % 4)) & 0x03 ;

	//char outputFilename[] = "d:\\out.bmp";

	//fb.open(outputFilename, ios::out);

	//*if (!outFile) {
	//	return;
	//}*/
	//ostream outFile(&fb);


	//outFile << 'B' << 'M' ;
	//Write_Long (14 + 40 + ((render_width * 24 + 31) / 32) * 4 * render_height, &outFile) ;
	//Write_Short (0, &outFile) ;
	//Write_Short (0, &outFile) ;
	//Write_Long (14 + 40, &outFile) ;
	//Write_Long (40, &outFile) ;
	//Write_Long (render_width, &outFile) ;
	//Write_Long (render_height, &outFile) ;
	//Write_Short (1, &outFile) ;
	//Write_Short (24, &outFile) ;
	//Write_Long (BI_RGB, &outFile) ;
	//Write_Long ((render_width * 24 + 31) / 32 * 4 * render_height, &outFile) ;
	//Write_Long (0, &outFile) ;
	//Write_Long (0, &outFile) ;
	//Write_Long (0, &outFile) ;
	//Write_Long (0, &outFile) ;

	////magic = (unsigned char *) POV_MALLOC (((render_width * 24 + 31) / 32) * 4 , "BMP magic") ;
	////memset (magic, 0, ((render_width * 24 + 31) / 32) * 4) ;
	////magic [0] = MAGIC1 ;
	////magic [1] = MAGIC2 ;
	////magic [2] = MAGIC3 ;
	////for (int i = 0 ; i < render_height ; i++)
	////  out.write ((char *) magic, (render_width * 24 + 31) / 32 * 4) ;
	////POV_FREE (magic) ;


	////if (!out.seekg (14 + 40 + (render_height - 1) * (3 * render_width + pad)))
	//// return;
	////Error ("Error seeking in BMP image.") ;

	////outFile << "test";
	//for (int i = 0 ; i <render_height ; i++)
	//{
	//	uchar* p = render_bitmap_surface + (render_bitmap.header.biHeight - 1 - i) * render_bitmap_bpl;
	//	outFile.seekp (14 + 40 + (render_height - 1 - i) * (3 * render_width + pad));
	//	for (int j=0; j<render_width; j++)
	//	{

	//		outFile << (unsigned char)  floor (*p++/maxclrbackup * 255.0);
	//		outFile << (unsigned char) floor (*p++/maxclrbackup * 255.0) ;
	//		outFile << (unsigned char) floor (*p++/maxclrbackup * 255.0) ;

	//		/*outFile << (unsigned char) floor (*p++ * 255.0) ;
	//		outFile << (unsigned char) floor (*p++ * 255.0) ;
	//		outFile << (unsigned char) floor (*p++ * 255.0) ;*/

	//		//float b = *p++;
	//		//float g = *p++;
	//		//float r = *p++;

	//		//b=b/255;
	//		//g=g/255;
	//		//r=r/255;
	//		//
	//		//outFile << (unsigned char) floor (b * 255.0) ;
	//		//outFile << (unsigned char) floor (g * 255.0) ;
	//		//outFile << (unsigned char) floor (r * 255.0) ;


	//		//unsigned char b = *p++;
	//		//unsigned char g = *p++;
	//		//unsigned char r = *p++;
	//		//char outval [3];
	//		//wsprintf(outval, "%c%c%c",b,g,r);
	//		//outFile << outval;
	//		//outFile << (unsigned char) *p++ ;
	//		//outFile << (unsigned char) *p++ ;
	//		//outFile << (unsigned char) *p++ ;
	//		for (int j = 0 ; j < pad; j++)
	//			outFile << (unsigned char) 0 ;
	//	}
	//}

	//fb.close();

	////if (!out)
	////  Error ("Error writing to BMP image.") ;
	////out.flush () ;
}